package com.itrui.upload.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itrui.upload.common.Result;
import com.itrui.upload.pojo.Picture;
import com.itrui.upload.service.PictureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/picture")
//批量对象保存图片，获取对象图片，删除对象图片
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @GetMapping("/{id}/{type}")
    public List<Picture> getPicture(@PathVariable("id") Long id, @PathVariable("type") int type, HttpServletRequest request){
        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        switch (type){
            //商品
            case 1:
                queryWrapper.eq(Picture::getProductId,id).eq(Picture::getType,type);
                break;
            //评论
            case 2:
                queryWrapper.eq(Picture::getCommentId,id).eq(Picture::getType,type);
                break;
            //头像
            case 3:
                queryWrapper.eq(Picture::getUserId,id).eq(Picture::getType,type);
                break;
        }
        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        List<Picture> list = pictureService.list(queryWrapper);
        list = list.stream().map((item)->{
            String _uri = item.getUri();
            item.setUri(url + _uri);
            return item;
        }).collect(Collectors.toList());

        return list;
    }

    @PostMapping("/{type}/{id}")
    public boolean savePicture(@RequestParam List<String> url, @PathVariable int type, @PathVariable Long id){
        List<Picture> pictures = new ArrayList<>();
        for (int i = 0; i < url.size(); i++){
            Picture picture = new Picture();
            if (type == 1){
                picture.setProductId(id);
                picture.setType(type);
                picture.setUri(url.get(i));
            } else if (type == 2){
                picture.setCommentId(id);
                picture.setType(type);
                picture.setUri(url.get(i));
            } else {
                picture.setUserId(id);
                picture.setType(type);
                picture.setUri(url.get(i));
            }
            pictures.add(picture);
        }

        boolean b = pictureService.saveBatch(pictures);
        return b?true:false;
    }

    @DeleteMapping("/{id}/{type}")
    public boolean deletePicture(@PathVariable Long id, @PathVariable int type){
        LambdaQueryWrapper<Picture> queryWrapper = new LambdaQueryWrapper<>();
        switch (type){
            //商品
            case 1:
                queryWrapper.eq(Picture::getProductId,id).eq(Picture::getType,type);
                break;
            //评论
            case 2:
                queryWrapper.eq(Picture::getCommentId,id).eq(Picture::getType,type);
                break;
            //头像
            case 3:
                queryWrapper.eq(Picture::getUserId,id).eq(Picture::getType,type);
                break;
        }
        boolean remove = pictureService.remove(queryWrapper);
        return remove?true:false;
    }

}

